﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using cnFederal.FaCode;

namespace cnFederal.Admin
{
    public partial class EditSelectedVideo : System.Web.UI.Page
    {
        Fvideo _fvideo;
        List<CnfiUser> _cnfiUsers;
        CultureInfo _ci;
        string _imageUrl;
        private bool _display;
        public EditSelectedVideo()
        {
            _ci = new CultureInfo("nb-NO");
            CnfiUsers.Load();
            CnfImages.Load();
            Fvideos.Load(true,true);
            _cnfiUsers = CnfiUsers.GetAllUsers();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = -1;
            
            string query = Request.QueryString["Id"];
            if (string.IsNullOrWhiteSpace(query))
                txtRegDate.Text = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day;
            if (!IsPostBack)
                InitializeListbox();
            if (!string.IsNullOrWhiteSpace(query))
            {

                try
                {
                    id = int.Parse(query);
                }
                catch (Exception ex) { }
                if (id != -1)
                    Load(id);

            }
            if (Session["ImageUrl"] != null)
                this._imageUrl = Session["ImageUrl"].ToString();
            MainTable.Visible = UserIdentity.Autenticated;
        }


        public void Load(int id)
        {
            _fvideo = Fvideos.GetVideoById(id);
            if (!IsPostBack && _fvideo != null)
            {
                DropDownListAuthor.SelectedValue = Convert.ToString(_fvideo.UserId);
                DropDownListType.SelectedValue = _fvideo.FileType;
                txtRegDate.Text = _fvideo.DateRegistered.Year + "-" + _fvideo.DateRegistered.Month + "-" + _fvideo.DateRegistered.Day;
                txtTitle.Text = _fvideo.Title;
                txtDateStartPublish.Text = _fvideo.DateStartPublish.Year + "-" + _fvideo.DateStartPublish.Month + "-" + _fvideo.DateStartPublish.Day;
                txtEndPublish.Text = _fvideo.DateEndPublish.Year + "-" + _fvideo.DateEndPublish.Month + "-" + _fvideo.DateEndPublish.Day;
                
                txtYoutubeCode.Text = _fvideo.FilePath;
                if (_fvideo.IsVisible == true)
                    RadioButtonEnable.Checked = true;
                else
                    RadioButtonDisable.Checked = true;
                
            }
        }
        protected void txtRegDate_TextChanged(object sender, EventArgs e)
        {



        }

        protected void txtDateStartPublish_TextChanged(object sender, EventArgs e)
        {
            //_fapost.DateStartPublish = DateTime.Parse(txtDateStartPublish.Text);


        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (_fvideo == null)
                CreateNew();
            else
                Update();
            Response.Redirect(AbsoluteURI);
        }

        public string AbsoluteURI
        {
            get
            {
                
                return HttpContext.Current.Request.Url.AbsoluteUri;
            }
        }
        private void Update()
        {
          
            if (!string.IsNullOrWhiteSpace(DropDownListType.SelectedValue))
                _fvideo.FileType = DropDownListType.SelectedValue;
            _fvideo.DateEndPublish = DateTime.Parse(txtEndPublish.Text, _ci);
            _fvideo.DateStartPublish = DateTime.Parse(txtDateStartPublish.Text, _ci);
            if(!string.IsNullOrWhiteSpace(DropDownListAuthor.SelectedValue))
            _fvideo.UserId = Convert.ToInt32(DropDownListAuthor.SelectedValue);
            if (!string.IsNullOrWhiteSpace(DropDownListType.SelectedValue))
                _fvideo.FileType = DropDownListType.SelectedValue;
            _fvideo.FilePath = txtYoutubeCode.Text;
            _fvideo.Title = txtTitle.Text;
            if (RadioButtonEnable.Checked)
                _fvideo.IsVisible = true;
            else
                _fvideo.IsVisible = false;
            Fvideos.Save(_fvideo);
        }

        private void CreateNew()
        {

            _fvideo = new Fvideo();
           
            _fvideo.FileType = DropDownListType.SelectedValue;
            _fvideo.DateEndPublish = DateTime.Parse(txtEndPublish.Text, _ci);
            _fvideo.DateRegistered = DateTime.Now;
            _fvideo.DateStartPublish = DateTime.Parse(txtDateStartPublish.Text, _ci);
            _fvideo.FilePath = txtYoutubeCode.Text;
            _fvideo.Title = txtTitle.Text;
            _fvideo.IsVisible = RadioButtonEnable.Checked;
            _fvideo.UserId = _AuthorId;
            Fvideos.CreateNew(_fvideo);


        }

        private void InitializeListbox()
        {

            if (_cnfiUsers != null && _cnfiUsers.Count != 0)
            {

                foreach (CnfiUser u in _cnfiUsers)
                {
                    ListItem item = new ListItem(u.Name, u.Id.ToString());
                    DropDownListAuthor.Items.Add(item);
                }
                ListItem nullItem = new ListItem("", "");
                nullItem.Selected = true;
                DropDownListAuthor.Items.Add(nullItem);

            }

        }

        protected void RadioButtonEnable_CheckedChanged(object sender, EventArgs e)
        {
            Session["display"] = true;
            if (RadioButtonDisable.Checked)
                _display = true;
            else
                _display = false;
        }

        protected void RadioButtonDisable_CheckedChanged(object sender, EventArgs e)
        {
            Session["display"] = false;
            if(RadioButtonDisable.Checked)
            _display = false;
            else
                _display = true;

        }
        private int _AuthorId;
        protected void DropDownListAuthor_SelectedIndexChanged(object sender, EventArgs e)
        {
            _AuthorId = Convert.ToInt32(DropDownListAuthor.SelectedValue);
        }


    }
}